home *** CD-ROM | disk | FTP | other *** search
- /*
- * @(#)runec.c 1.3 10/24/89
- */
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/file.h>
- #include <sys/uio.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <stdio.h>
-
- #ifndef EMDIR
- #define EMDIR "/scratch/eric/emerald/"
- #endif
-
- extern int errno;
-
- #include "Kernel/h/emPorts.h"
-
- #define KERNELREQUESTSIZE 16
- #define IFLAGINDEX 10
-
- #include <netdb.h>
- #include <signal.h>
-
- int talkSock;
- struct sockaddr_in destSockDescr = {AF_INET};
- int vflag = 0;
- int iflag = 1;
- int mflag = 0;
- char mdata[100];
- #define NAMELIMIT 80
- char name[NAMELIMIT];
- int namelen;
- char *filename = NULL;
- char *MLP1 = NULL, *MLP2 = NULL;
-
- #ifdef STARTKERNEL
- char *startKernelArgs[] = {
- "EmeraldKernel",
- (char *) 0,
- (char *) 0,
- };
- extern int callsys();
- #endif
-
- int sendLoadDotoRequest()
- {
- struct hostent *myHost;
- int result = 1;
- #ifdef STARTKERNEL
- char binname[100];
- int try = 0
-
- again:
- if (++try > 2) {
- result = 0;
- goto cleanup;
- }
- #endif
-
- talkSock = socket(AF_INET, SOCK_STREAM, 0);
- if (talkSock < 0) {
- perror("runec: socket");
- return(0);
- }
-
- if (! mflag) {
- if (gethostname(mdata, (int *)100) < 0) {
- perror("runec: gethostname");
- result = 0;
- goto cleanup;
- }
- }
- myHost = gethostbyname(mdata);
- if (myHost == NULL) {
- perror("runec: gethostbyname");
- result = 0;
- goto cleanup;
- }
-
- bcopy(myHost->h_addr, (char *)&destSockDescr.sin_addr,
- (unsigned)myHost->h_length);
-
-
- {
- char *emplanename;
- int emPlane = 0;
- int emPort = EMKERNELDEFAULTPORTNUMBER;
- struct servent *myService;
- emplanename = (char *) getenv("EMPLANE");
- if (emplanename != NULL) emPlane = atoi(emplanename);
- #ifdef DIKU
- myService = getservbyname(EMKERNELSERVICEPORT, "tcp");
- if(myService)
- emPort = ntohs(myService->s_port);
- #endif
- destSockDescr.sin_port = htons(emPort + emPlane*4 + 2);
- }
-
- if (connect(talkSock, (struct sockaddr *)&destSockDescr,
- sizeof(destSockDescr)) == -1) {
- #ifdef STARTKERNEL
- perror("sendToKernel: (connect)");
- fprintf(stderr, "Trying to re-start kernel\n");
- sprintf(binname, "%s/bin/startKernel", EMDIR);
- if (mflag) startKernelArgs[1] = mdata;
- callsys(binname, startKernelArgs, (char *)NULL,
- (char *)NULL);
- fprintf(stderr, "Started kernel, trying again.\n");
- if (close(talkSock) < 0) perror("close");
- goto again;
- #else
- perror("runec: connect");
- result = 0;
- goto cleanup;
- #endif
- }
-
- if (vflag) printf("Connected to %d\n", ntohs(destSockDescr.sin_port));
-
- if (vflag) printf("writing \"%.*s\"\n", namelen, name);
- if (write(talkSock, name, namelen) != namelen) {
- perror("runec: write");
- result = 0;
- goto cleanup;
- }
-
- cleanup:
- if (!result) {
- (void) shutdown(talkSock, 2);
- (void) close(talkSock);
- }
- return(result);
- }
-
- char *Usage = "Usage:\trunec <flags> <filename>\n\
- flags:\n\
- \t[-v] [-[mM] <machine name>] [-i] [-b]\n";
-
- static void usagePanic()
- {
- fprintf(stderr, Usage);
- exit(1);
- }
-
- parseFlags(argc, argv)
- int argc;
- char **argv;
- {
- register int i;
- FILE *inputFile;
- char *newfilename;
-
- for (i = 1; i < argc; i++) {
- if (argv[i][0] == '-') {
- switch (argv[i][1]) {
- case 'v':
- vflag = 1;
- break;
- case 'i':
- iflag = 1;
- break;
- case 'b':
- iflag = 0;
- break;
- case 'm':
- case 'M':
- if (argv[i][2] == '\0') {
- i++;
- if (i < argc) {
- strcpy(mdata, argv[i]);
- } else {
- usagePanic();
- }
- } else {
- strcpy(mdata, &argv[i][2]);
- }
- mflag = 1;
- break;
- case 'X':
- if (i < argc - 3) {
- i++;
- if (filename != NULL) usagePanic();
- filename = argv[i];
- i++;
- MLP1 = argv[i];
- i++;
- MLP2 = argv[i];
- } else {
- usagePanic();
- }
- break;
- default:
- fprintf(stderr, "Unknown flag %s\n", argv[i]);
- usagePanic();
- break;
- }
- } else if (filename == NULL) {
- filename = argv[i];
- } else {
- usagePanic();
- }
- }
- if (filename == NULL) {
- usagePanic();
- } else {
- if (!strcmp(filename+strlen(filename)-2, ".m")) {
- filename[strlen(filename)-1] = 'g';
- newfilename = filename;
- } else if (!strcmp(filename+strlen(filename)-2, ".g")) {
- newfilename = filename;
- } else {
- newfilename = (char *) malloc(strlen(filename)+5);
- sprintf(newfilename, "%s.g", filename);
- }
- inputFile = fopen(newfilename, "r");
- if (inputFile == NULL) {
- fprintf(stderr, "Can't find \"%s\".\n", newfilename);
- usagePanic();
- }
- fgets(name, 80, inputFile);
- if (*name == '#') fgets(name, 80, inputFile);
- if (*name && name[strlen(name)-1] == '\n') name[strlen(name)-1] = '\0';
- if (strlen(name) != KERNELREQUESTSIZE) {
- fprintf(stderr,
- "File %s is not an Emerald compiler output file (wrong size).\n", newfilename);
- usagePanic();
- }
- fclose(inputFile);
- if (strncmp(name, "0x", 2)) {
- fprintf(stderr, "File %s is not an Emerald compiler output file (bad stuff).\n",
- newfilename);
- usagePanic();
- }
- name[IFLAGINDEX] = (iflag ? 'T' : 'F');
- namelen = KERNELREQUESTSIZE;
- /*
- * Stuff to add MLP arguments goes here
- */
- if (MLP1 && MLP2) {
- sprintf(&name[namelen], "%4d", strlen(MLP1));
- namelen += 4;
- bcopy(MLP1, &name[namelen], strlen(MLP1));
- namelen += strlen(MLP1);
- bcopy(MLP2, &name[namelen], strlen(MLP2));
- namelen += strlen(MLP2);
- if (namelen > NAMELIMIT) {
- fprintf(stderr, "Generated name is too long\n");
- usagePanic();
- }
- }
- }
- }
-
- char buffer[BUFSIZ];
-
- doit(in, out)
- int in, out;
- {
- int nBytes;
- while ((nBytes = read(in, buffer, BUFSIZ)) > 0) {
- if (write(out, buffer, nBytes) < 0) break;
- }
- }
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- int child;
- parseFlags(argc, argv);
- if (sendLoadDotoRequest()) {
- if (iflag) {
- child = fork();
- if (child < 0) {
- perror("Runec: fork");
- exit(1);
- }
- if (child > 0) {
- doit(talkSock, 1);
- if (shutdown(talkSock, 0) < 0) {
- perror("runec (shutdown)");
- exit(1);
- }
- kill(child, SIGKILL);
- while (wait(0) != child) ;
- } else{
- doit(0, talkSock);
- if (shutdown(talkSock, 1) < 0) {
- perror("runec (shutdown)");
- exit(1);
- }
- }
- }
- }
- }
-
- #ifdef STARTKERNEL
- /*
- * Call the program named f with the arguments v, and stdin and out
- * directed to inf and outf respectively.
- */
- #include <sys/wait.h>
-
- int callsys(f, v, inf, outf)
- char *f, **v, *inf, *outf;
- {
- int t;
- union wait status;
- register char **cpp;
-
- t = vfork();
- if (t == -1) {
- fprintf(stderr, "No more processes\n");
- return (100);
- }
- if (t == 0) { /* I am the child */
- int infd, outfd;
- if (inf != NULL) {
- infd = open(inf, O_RDONLY, 0666);
- (void) dup2(infd, 0);
- }
- if (outf != NULL) {
- outfd = open(outf, O_WRONLY | O_CREAT | O_TRUNC, 0666);
- (void) dup2(outfd, 1);
- }
- execv(f, v);
- fprintf(stderr, "Can't find %s\n", f);
- (void) fflush(stderr);
- _exit(100);
- }
- while (t != wait(&status))
- ;
- if ((t=status.w_termsig) != 0 && t!=14) {
- if (t!=2) {
- fprintf(stderr, "Fatal error in %s\n", f);
- }
- }
- return (status.w_retcode);
- }
- #endif
-